<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Receives data from a socket, connected or not</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-pair.html">stream_socket_pair</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-sendto.html">stream_socket_sendto</a></div>
 <div class="up"><a href="ref.stream.html">Stream 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.stream-socket-recvfrom" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_recvfrom</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">stream_socket_recvfrom</span> &mdash; <span class="dc-title">Receives data from a socket, connected or not </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-socket-recvfrom-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>stream_socket_recvfrom</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$address</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>stream_socket_recvfrom()</strong></span> accepts
   data from a remote socket up to <em><code class="parameter">length</code></em> bytes.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-recvfrom-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">socket</code></em></dt>

     <dd>

      <p class="para">
       The remote socket.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">length</code></em></dt>

     <dd>

      <p class="para">
       The number of bytes to receive from the <em><code class="parameter">socket</code></em>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       The value of <em><code class="parameter">flags</code></em> can be any combination
       of the following:
       <table class="doctable table">
        <caption><strong>Possible values for <em><code class="parameter">flags</code></em></strong></caption>
        
         <tbody class="tbody">
          <tr>
           <td><strong><code>STREAM_OOB</code></strong></td>
           <td>
            Process OOB (<em>out-of-band</em>) data.
           </td>
          </tr>

          <tr>
           <td><strong><code>STREAM_PEEK</code></strong></td>
           <td>
            Retrieve data from the socket, but do not consume the buffer.
            Subsequent calls to <span class="function"><a href="function.fread.html" class="function">fread()</a></span> or
            <span class="function"><strong>stream_socket_recvfrom()</strong></span> will see
            the same data.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">address</code></em></dt>

     <dd>

      <p class="para">
       If <em><code class="parameter">address</code></em> is provided it will be populated with
       the address of the remote socket.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-recvfrom-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the read data, as a string
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-socket-recvfrom-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3942">
    <p><strong>Example #1 <span class="function"><strong>stream_socket_recvfrom()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Open&nbsp;a&nbsp;server&nbsp;socket&nbsp;to&nbsp;port&nbsp;1234&nbsp;on&nbsp;localhost&nbsp;*/<br /></span><span style="color: #0000BB">$server&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_server</span><span style="color: #007700">(</span><span style="color: #DD0000">'tcp://127.0.0.1:1234'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Accept&nbsp;a&nbsp;connection&nbsp;*/<br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_accept</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Grab&nbsp;a&nbsp;packet&nbsp;(1500&nbsp;is&nbsp;a&nbsp;typical&nbsp;MTU&nbsp;size)&nbsp;of&nbsp;OOB&nbsp;data&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Received&nbsp;Out-Of-Band:&nbsp;'"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1500</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_OOB</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Take&nbsp;a&nbsp;peek&nbsp;at&nbsp;the&nbsp;normal&nbsp;in-band&nbsp;data,&nbsp;but&nbsp;don't&nbsp;comsume&nbsp;it.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Data:&nbsp;'"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1500</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_PEEK</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;exact&nbsp;same&nbsp;packet&nbsp;again,&nbsp;but&nbsp;remove&nbsp;it&nbsp;from&nbsp;the&nbsp;buffer&nbsp;this&nbsp;time.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"Data:&nbsp;'"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">stream_socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1500</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"'\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Close&nbsp;it&nbsp;up&nbsp;*/<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.stream-socket-recvfrom-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If a message received is longer than the <em><code class="parameter">length</code></em>
    parameter, excess bytes may be discarded depending on the type of socket
    the message is received from (such as UDP).
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Calls to <span class="function"><strong>stream_socket_recvfrom()</strong></span> on socket-based
    streams, after calls to buffer-based stream functions (like
    <span class="function"><a href="function.fread.html" class="function">fread()</a></span> or <span class="function"><a href="function.stream-get-line.html" class="function">stream_get_line()</a></span>) read
    data directly from the socket and bypass the stream buffer.
    </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-socket-recvfrom-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.stream-socket-sendto.html" class="function" rel="rdfs-seeAlso">stream_socket_sendto()</a> - Sends a message to a socket, whether it is connected or not</span></li>
   <li class="member"><span class="function"><a href="function.stream-socket-client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
   <li class="member"><span class="function"><a href="function.stream-socket-server.html" class="function" rel="rdfs-seeAlso">stream_socket_server()</a> - Create an Internet or Unix domain server socket</span></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.stream-socket-pair.html">stream_socket_pair</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.stream-socket-sendto.html">stream_socket_sendto</a></div>
 <div class="up"><a href="ref.stream.html">Stream 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
